import XEUtils from 'xe-utils'
import VXETable from 'vxe-table'
// 自定义全局的格式化处理函数
VXETable.formats.mixin({
    // 格式化性别
    formatSex({ cellValue }) {
        return cellValue ? (cellValue === '1' ? '男' : '女') : ''
    },
    // 格式化下拉选项
    formatSelect({ cellValue, row, column }, colConfig) {
        let val = "id";
        let name = "label";
        let childs = "children";
        let showName = "label";
        if (colConfig.props) {
            val = colConfig.props.id || "id";
            name = colConfig.props.label || "label";
            childs = colConfig.props.children || "children";
            showName = colConfig.props.showType || "label";
        }
        let showReturnName = "";
        if (colConfig.options && colConfig.options.length > 0) {
            // console.log(222, column.own.code, colConfig.multiple);
            if (colConfig.multiple) {
                const treeArrayData = XEUtils.toTreeArray(colConfig.options, { children: childs })
                let itemData = XEUtils.filter(treeArrayData, item => XEUtils.includes(cellValue, item[val]));
                if (showName == "label") {
                    let itemNameData = XEUtils.map(itemData, item => item[name]);
                    showReturnName = itemNameData && itemNameData.length > 0 ? itemNameData.join(",") : "";
                } else if (showName == "id-label") {
                    let itemIdNameData = XEUtils.map(itemData, item => `${item[val]}-${item[name]}`);
                    showReturnName = itemIdNameData && itemIdNameData.length > 0 ? itemIdNameData.join(",") : "";
                } else {
                    showReturnName = cellValue && cellValue.length > 0 ? cellValue.join(",") : "";
                }
            } else {
                let itemObj = XEUtils.findTree(colConfig.options, item => item[val] === cellValue, { children: childs });
                if (showName == "label") {
                    return itemObj && itemObj.item && itemObj.item[name] ? itemObj.item[name] : "";
                } else if (showName == "id-label") {
                    return itemObj && itemObj.item && itemObj.item[name] ? `${cellValue}-${itemObj.item[name]}` : cellValue || ""
                } else {
                    return cellValue || "";
                }
            }
        }
        return showReturnName;

    },
    // 格式化日期，默认 yyyy-MM-dd HH:mm:ss
    formatDate({ cellValue }, format) {
        return XEUtils.toDateString(cellValue, format || 'yyyy-MM-dd HH:mm:ss')
    },
    // 四舍五入金额，每隔3位逗号分隔，默认2位数
    formatAmount({ cellValue }, digits = 2) {
        return XEUtils.commafy(Number(cellValue), { digits })
    },
    // 格式化银行卡，默认每4位空格隔开
    formatBankcard({ cellValue }) {
        return XEUtils.commafy(XEUtils.toValueString(cellValue), { spaceNumber: 4, separator: ' ' })
    },
    // 四舍五入,默认两位数
    formatFixedNumber({ cellValue }, digits = 2) {
        return XEUtils.toFixed(XEUtils.round(cellValue, digits), digits)
    },
    // 向下舍入,默认两位数
    formatCutNumber({ cellValue }, digits = 2) {
        return XEUtils.toFixed(XEUtils.floor(cellValue, digits), digits)
    },
    // 转换 moment 类型为字符串
    toMomentString({ cellValue }, format) {
        return cellValue ? cellValue.format(format) : ''
    },
})
